package com.jcfk.eam.controller.mould;

import cn.hutool.core.util.StrUtil;
import com.jcfk.common.business.log.annotation.RequestLogHeader;
import com.jcfk.common.web.domain.*;
import com.jcfk.eam.domain.dto.mould.MouldBomDetailDTO;
import com.jcfk.eam.domain.query.mould.MouldBomDetailQuery;
import com.jcfk.eam.service.mould.MouldBomDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 模具bom详情表
 *
 * @author yrl
 * @email 
 * @date 2024-11-08
 */
@Api(tags = {"模具bom详情表管理"})
@RestController
@RequestMapping("modbomdetail")
public class MouldBomDetailController {
    @Autowired
    private MouldBomDetailService modBomDetailService;

    /**
     * 分页列表
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "ReqPageParam<ModBomDetailQuery>", name = "requestParam", value = "", required = true)
    })
    @ApiOperation("分页查询模具bom详情表")
    @PostMapping("/queryByPage")
    public RetPage<MouldBomDetailDTO> queryByPage(@RequestBody ReqPageParam<MouldBomDetailQuery> requestParam) {
        //设置默认排序
        if(requestParam.getSort().getOrderItem()==null){
            ReqSortParam<MouldBomDetailQuery> sortParam=new ReqSortParam();
            sortParam.setColumn("lastUpdateTime");
            sortParam.setOrderBy("desc");
            requestParam.setSort(sortParam);
        }
        DataPage<MouldBomDetailDTO> page = new DataPage<MouldBomDetailDTO>((long) requestParam.getCurrent(),
                (long) requestParam.getSize(),
                requestParam.getSort().getOrderItem(),
            MouldBomDetailDTO.class);
        page = modBomDetailService.queryByPage(page, requestParam.getData());

        return new RetPage<>(page);
    }


    /**
     *  模具bom详情表
     * 根据id 查询详情
     */
    @ApiOperation("查询模具bom详情表")
    @GetMapping("/get/{id}")
    public RetResult<MouldBomDetailDTO> get(@PathVariable("id") String id) {
            MouldBomDetailDTO item = modBomDetailService.get(id);
        return RetResult.ok(item);
    }

    /**
     * 添加/更新管理模具bom详情表
     */
    @ApiOperation("保存模具bom详情表")
    @PostMapping("/save")
    @RequestLogHeader(modular = "模具bom详情表管理", businessType = "保存")
    public RetResult<?> save(@RequestBody @Valid MouldBomDetailDTO modBomDetail) {
        // 数据校验
        RetResult ret = modBomDetailService.isValid(modBomDetail);
        if (!ret.getCode().equals(RetResult.SUCCESS)) {
            return ret;
        }

        if (StrUtil.isEmpty(modBomDetail.getId())){
            // 新增
            modBomDetailService.insert(modBomDetail);
        } else{
            // 更新
            modBomDetailService.update(modBomDetail);
        }
        return RetResult.ok();

    }

    /**
     *  批量删除模具bom详情表
     */
    @ApiOperation("批量删除模具bom详情表")
    @PostMapping("/delete")
    @RequestLogHeader(modular = "模具bom详情表管理", businessType = "批量删除")
    public RetResult<?> delete(@RequestBody MouldBomDetailDTO modBomDetail) {
        return RetResult.ok(modBomDetailService.logicDeleteList(modBomDetail));
    }





    /**
     *  删除模具bom详情表
     */
    @ApiOperation("删除模具bom详情表")
    @PostMapping("/deleteItem/{id}")
    @RequestLogHeader(modular = "模具bom详情表管理", businessType = "删除")
    public RetResult<?> deleteItem(@PathVariable("id") String id) {
        modBomDetailService.deleteItem(id);
        return RetResult.ok();
    }


    /**
     *  查询bom树形结构并按照数量复制节点
     */
    @ApiOperation("查询bom树形结构并按照数量复制节点")
    @GetMapping("/queryTreeCopyByNum/{bomId}")
    @RequestLogHeader(modular = "模具bom详情表管理")
    public RetResult<?> queryTreeCopyByNum(@PathVariable("bomId") String bomId) {

        return RetResult.ok(modBomDetailService.queryTreeCopyByNum(bomId));
    }
}
